package com.uphone.kingmall.fragment.business_area;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;

import com.china.zhuoyue.R;
import com.just.agentweb.AgentWeb;
import com.uphone.kingmall.adapter.RvShopGoodsAdapter;
import com.uphone.kingmall.base.BaseFragment;
import com.uphone.kingmall.bean.ShopGoodsBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * <pre>
 *     author : 孙增艳
 *     e-mail : sunzengyan@outlook.com
 *     time   : 2019/2/28
 *     desc   : 店铺首页
 *     version: 1.0
 * </pre>
 */
public class ShopHomeFragment extends BaseFragment {

    private static ShopHomeFragment fragment;
    //    @BindView(R.id.rv_shop_home)
//    RecyclerView rvShopHome;
    @BindView(R.id.ll)
    RelativeLayout mLlWeb;

    private List<ShopGoodsBean> shopGoodsBeanList = new ArrayList<>();
    private RvShopGoodsAdapter shopGoodsAdapter;
    private int webHeight = 0;
    private AgentWeb.PreAgentWeb ready;

    public static ShopHomeFragment getFragment() {
        return fragment;
    }

    public static ShopHomeFragment newInstance() {
        if (fragment == null) {
            fragment = new ShopHomeFragment();
            Bundle bundle = new Bundle();
            fragment.setArguments(bundle);
        }
        return fragment;
    }

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_fragment_shop_home;
    }

    @Override
    public void initData() {
    }

    @Override
    public void initView(View view) {
    }

    public void setUrl(String url) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final WebView webView = new WebView(getActivity());
        mLlWeb.addView(webView, 0, params);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);//开启JavaScript交互
        webSettings.setUseWideViewPort(true);//开启页面自适应屏幕
        webSettings.setLoadWithOverviewMode(true);//
        webSettings.setSupportZoom(false);//关闭双击缩放
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

//        webSettings.setTextSize(WebSettings.TextSize.LARGEST);
        webSettings.setBuiltInZoomControls(false);//缩放控制条
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);//离线缓存功能
        webSettings.setAllowFileAccess(true);//允许跨域访问本地文件
        webSettings.setAppCacheEnabled(true);//允许Web缓存到本地
        webSettings.setDomStorageEnabled(true);//设置DOM Storage缓存
        webSettings.setDatabaseEnabled(true);//允许DataBase存储
        webSettings.setGeolocationEnabled(true);//启用地理定位

//        webSettings.setStandardFontFamily(getResources().getFont().);
//        webSettings.setUserAgentString("User-Agent:PengZhang");
        webSettings.setAllowUniversalAccessFromFileURLs(true);
//        final String dbPath = MyApplication.getInstance().getDir("db", Context.MODE_PRIVATE).getPath();
//        webSettings.setDatabasePath(dbPath);//Storage路径
        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);//设置渲染优先级
        webSettings.setPluginState(WebSettings.PluginState.ON);//设置插件状态
        webSettings.setAppCacheEnabled(true);
//        final String cachePath = MyApplication.getInstance().getDir("cache", Context.MODE_PRIVATE).getPath();
//        webSettings.setAppCachePath(cachePath);//cache路径
        webSettings.setAppCacheMaxSize(5 * 1024 * 1024);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                int w = View.MeasureSpec.makeMeasureSpec(0,
                        View.MeasureSpec.UNSPECIFIED);
                int h = View.MeasureSpec.makeMeasureSpec(0,
                        View.MeasureSpec.UNSPECIFIED);
                // 重新测量
                webView.measure(w, h);
            }
        });
        webView.loadUrl(url);
    }
}
